package com.shensi.appCollect.controller;

import com.alibaba.fastjson.JSONObject;
import com.shensi.appCollect.pojo.CustomerElectronicDeputyCard;
import com.shensi.appCollect.pojo.DeputyCardRule;
import com.shensi.appCollect.pojo.vo.ExchangeCardVo;
import com.shensi.appCollect.service.DeputyCardRuleService;
import com.shensi.appCollect.service.ElectronicDeputyCardService;
import com.shensi.appCollect.utils.ResultData;
import com.shensi.appCollect.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/electronic/deputy")
@Slf4j
@Api(tags = "电子卡副卡")
public class ElectronicDeputyController {

    @Autowired
    private ElectronicDeputyCardService electronicDeputyCardService;

    @Autowired
    private DeputyCardRuleService deputyCardRuleService;


    // 查询会员所有副卡
    @PostMapping("/allDeputy/list")
    @ApiOperation(value = "查询会员所有副卡",httpMethod = "POST")
    public ResultData selectElectronicDeputyCardList(@RequestBody CustomerElectronicDeputyCard customerElectronicDeputyCard){
        if(Objects.isNull(customerElectronicDeputyCard) || Objects.isNull(customerElectronicDeputyCard.getUserId())){
            return ResultData.error("请求参数异常");
        }
        log.info("查询会员所有副卡selectElectronicDeputyCardList请求参数:[{}]", JSONObject.toJSONString(customerElectronicDeputyCard));
        List<CustomerElectronicDeputyCard> customerElectronicDeputyCards = electronicDeputyCardService.selectElectronicDeputyCardList(customerElectronicDeputyCard);
        log.info("查询会员所有副卡selectElectronicDeputyCardList响应结果:[{}]", customerElectronicDeputyCards.size());
        return ResultData.success(customerElectronicDeputyCards);
    }


    // 扫二维码新增副卡
    @PostMapping("/add")
    @ApiOperation(value = "扫二维码新增副卡",httpMethod = "POST")
    public ResultData addElectronicDeputyCard(@RequestBody CustomerElectronicDeputyCard customerElectronicDeputyCard) {
        if (Objects.isNull(customerElectronicDeputyCard) || Objects.isNull(customerElectronicDeputyCard.getUserId())
                || Objects.isNull(customerElectronicDeputyCard.getRuleId())) {
            return ResultData.error("请求参数异常");
        }
        log.info("新增副卡addElectronicDeputyCard请求参数:[{}]",JSONObject.toJSONString(customerElectronicDeputyCard));
        CustomerElectronicDeputyCard electronicDeputyCard = electronicDeputyCardService.addElectronicDeputyCard(customerElectronicDeputyCard);
        log.info("新增副卡addElectronicDeputyCard返回结果:[{}]",JSONObject.toJSONString(electronicDeputyCard));

        return ResultData.success("已成功加入" + electronicDeputyCard.getMobilePhone() + "用户副卡",null);
    }


    // 删除副卡
    @GetMapping("/delete")
    @ApiOperation(value = "删除副卡",httpMethod = "GET")
    public ResultData deleteElectronicDeputyCard(@RequestParam Long id){
        if(Objects.isNull(id)){
            return ResultData.error("请求参数");
        }
        log.info("删除副卡 请求参数：[{}]",id);
        int i = electronicDeputyCardService.deleteElectronicDeputyCard(id);
        log.info("删除副卡 响应结果：[{}]",i);
        if(i<=0){
            return ResultData.error("删除副卡失败");
        }
        return ResultData.success(i);
    }



    // 回收副卡 金额归0+清除规则
    @GetMapping("/recovery")
    @ApiOperation(value = "回收副卡",httpMethod = "GET")
    public ResultData recoveryElectronicDeputyCard(@RequestParam Long id) {
        if (Objects.isNull(id)) {
            return ResultData.error("请求参数");
        }
        log.info("回收副卡 请求参数：[{}]",id);
        int i = electronicDeputyCardService.recoveryElectronicDeputyCard(id);
        log.info("回收副卡 响应结果：[{}]",i);
        if(i<=0){
            return ResultData.error("回收副卡失败");
        }
        return ResultData.success(i);
    }




    // 新增添加副卡规则
    @PostMapping("/rule/save")
    @ApiOperation(value = "新增添加副卡规则",httpMethod = "POST")
    public ResultData saveDeputyCardRule(@RequestBody DeputyCardRule deputyCardRule){
        if(Objects.isNull(deputyCardRule) || Objects.isNull(deputyCardRule.getMainId())){
            return ResultData.error("请求参数异常");
        }
        log.info("新增添加副卡规则saveDeputyCardRule请求参数：[{}]",JSONObject.toJSONString(deputyCardRule));
        Long ruleId = deputyCardRuleService.saveDeputyCardRule(deputyCardRule);
        log.info("新增添加副卡规则saveDeputyCardRule返回结果：[{}]",ruleId);
        return ResultData.success(ruleId);
    }

    // 根据主卡id 查询最新的添加副卡规则
    @PostMapping("/rule/get")
    @ApiOperation(value = "根据主卡id查询最新的添加副卡规则",httpMethod = "POST")
    public ResultData selectDeputyCardRule(@RequestBody DeputyCardRule deputyCardRule){
        if(Objects.isNull(deputyCardRule) || Objects.isNull(deputyCardRule.getMainId())){
            return ResultData.error("请求参数异常");
        }
        log.info("查询添加副卡规则selectDeputyCardRule请求参数：[{}]",JSONObject.toJSONString(deputyCardRule));
        DeputyCardRule result = deputyCardRuleService.selectDeputyCardRule(deputyCardRule);
        log.info("查询添加副卡规则selectDeputyCardRule响应结果：[{}]",JSONObject.toJSONString(result));
        return ResultData.success(result);
    }


    // 卡余额比实付金额小才需要换副卡
    @PostMapping("/exchange/card")
    @ApiOperation(value = "替换副卡支付",httpMethod = "POST")
    public ResultData exchangeCard(@RequestBody ExchangeCardVo exchangeCardVo){
        if(Objects.isNull(exchangeCardVo)){
            return ResultData.error("请求参数异常");
        }

        if(StringUtils.isEmpty(exchangeCardVo.getOrderNo())){
            return ResultData.error("请选择订单号");
        }

        if(Objects.isNull(exchangeCardVo.getUserId())){
            return ResultData.error("请选择用户id");
        }
        if(Objects.isNull(exchangeCardVo.getBalance()) || Objects.isNull(exchangeCardVo.getGenericBalance())){
            return ResultData.error("请选择卡余额");
        }
        log.info("替换副卡支付exchangeCard 请求参数：[{}]",JSONObject.toJSONString(exchangeCardVo));
        ExchangeCardVo result = electronicDeputyCardService.exchangeCard(exchangeCardVo);
        log.info("替换副卡支付exchangeCard 返回结果：[{}]",JSONObject.toJSONString(result));
        return ResultData.success(result);
    }

}
